package com.ruoyi.basebill.task;

import com.ruoyi.basebill.domain.*;
import com.ruoyi.basebill.service.impl.OmsPurinwarehsbillServiceImpl;
import com.ruoyi.basebill.service.impl.OmsStocktransferbillServiceImpl;
import com.ruoyi.basebill.service.impl.OmsSumpurinwarehsbillServiceImpl;
import com.ruoyi.basebill.service.impl.OmsSumstocktransferbillServiceImpl;
import com.ruoyi.basebill.task.util.OmsTaskUtil;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

@Component("OmsSumStockTransferBillTask")
public class OmsSumStockTransferBillTask {
    //注入自己
    @Autowired
    private OmsSumStockTransferBillTask sumStockTransferBillTask;

    //单据自动任务工具类
    @Autowired
    private OmsTaskUtil omsTaskUtil;

    //库存调拨单服务层接口
    @Autowired
    private OmsStocktransferbillServiceImpl stocktransferbillService;

    //汇总库存调拨单服务层接口
    @Autowired
    private OmsSumstocktransferbillServiceImpl sumstocktransferbillService;

    /**
     * 查出所有符合条件的库存调拨单，汇总生成汇总调拨单
     */
    @Transactional
    public void summary(){
        List<OmsStocktransferbill> omsStocktransferbills = stocktransferbillService.selectSumBillNumberIsNull();
        if (StringUtils.isEmpty(omsStocktransferbills)){
            return;
        }
        ArrayList<OmsSumstocktransferbill> omsSumstocktransferbills = new ArrayList<>();
        HashMap<String, ArrayList<Long>> sumBillNumberAndIds = new HashMap<>();
        sumStockTransferBillTask.summaryStockTransferBill(omsStocktransferbills,omsSumstocktransferbills,sumBillNumberAndIds);
        for (OmsSumstocktransferbill omsSumstocktransferbill : omsSumstocktransferbills) {
            ArrayList<OmsSumstocktransferbillentry> omsSumstocktransferbillentries = new ArrayList<>();
            ForA:
            for (OmsSumstocktransferbillentry omsSumstocktransferbillentry : omsSumstocktransferbill.getOmsSumstocktransferbillentryList()) {
                for (int i = 0; i < omsSumstocktransferbillentries.size(); i++) {
                    OmsSumstocktransferbillentry omsSumstocktransferbillentry1 = omsSumstocktransferbillentries.get(i);
                    boolean b = judgeSumBillEntry(omsSumstocktransferbillentry, omsSumstocktransferbillentry1);
                    if (b){
                        summarySumBillEntry(omsSumstocktransferbillentry,omsSumstocktransferbillentry1);
                        omsSumstocktransferbillentries.set(i,omsSumstocktransferbillentry1);
                        continue ForA;
                    }
                }
                omsSumstocktransferbillentries.add(omsSumstocktransferbillentry);
            }
            omsSumstocktransferbill.setOmsSumstocktransferbillentryList(omsSumstocktransferbillentries);
            sumstocktransferbillService.insertOmsSumstocktransferbill(omsSumstocktransferbill);
        }
        for (String s : sumBillNumberAndIds.keySet()) {
            stocktransferbillService.updateSumBillNumberByIds(s,sumBillNumberAndIds.get(s));
        }
    }

    /**
     * 传入库存调拨单集合、获取汇总库存调拨单集合以及对应的汇总单编码以及对应的原单id
     * @param omsStocktransferbills 库存调拨单集合
     * @param omsSumstocktransferbills 汇总库存调拨单集合
     * @param billNumberAndIds 汇总单编码和对应的原单id
     */
    public void summaryStockTransferBill(List<OmsStocktransferbill> omsStocktransferbills, ArrayList<OmsSumstocktransferbill> omsSumstocktransferbills, HashMap<String,ArrayList<Long>> billNumberAndIds){
        ArrayList<String> billNumbers = new ArrayList<>();

        //如果集合不为null且不为空
        //遍历集合，将id存储在hashset中，用于后续进行批量查询。
        HashSet<Long> idSet = new HashSet<Long>();
        HashMap<Long, OmsStocktransferbill> stockMap = new HashMap<Long, OmsStocktransferbill>();
        if(omsStocktransferbills!=null&& !omsStocktransferbills.isEmpty()) {
            for (int i = 0; i < omsStocktransferbills.size(); i++) {
                OmsStocktransferbill omsStocktransferbill = omsStocktransferbills.get(i);
                Long id = omsStocktransferbill.getId();
                idSet.add(id);
            }
            //根据id集合进行批量查询
            List<OmsStocktransferbill> omsStocktransferbillList = stocktransferbillService.selectOmsStocktransferbillByIds(idSet);
            //循环集合，将数据存储在map中，用于后续快速获取。
            if(omsStocktransferbillList!=null&& !omsStocktransferbillList.isEmpty()) {
                for (int i = 0; i < omsStocktransferbillList.size(); i++) {
                    OmsStocktransferbill omsStocktransferbill = omsStocktransferbillList.get(i);
                    Long id = omsStocktransferbill.getId();
                    stockMap.put(id, omsStocktransferbill);
                }
            }
        }
        for (int i = 0; i < omsStocktransferbills.size(); i++) {
            OmsStocktransferbill omsStocktransferbill = omsStocktransferbills.get(i);
            omsStocktransferbill = stockMap.get(omsStocktransferbill.getId());
            String summaryBillNumber = sumStockTransferBillTask.summarySTBill(omsStocktransferbill, omsSumstocktransferbills, billNumbers);
            Long id = omsStocktransferbill.getId();
            if (billNumberAndIds.containsKey(summaryBillNumber)){
                ArrayList<Long> tempIds = billNumberAndIds.get(summaryBillNumber);
                tempIds.add(id);
                billNumberAndIds.put(summaryBillNumber,tempIds);
            }else {
                ArrayList<Long> tempIds = new ArrayList<>();
                tempIds.add(id);
                billNumberAndIds.put(summaryBillNumber,tempIds);
                billNumbers.add(summaryBillNumber);
            }
        }
    }

    public String summarySTBill(OmsStocktransferbill omsStocktransferbill,
                                List<OmsSumstocktransferbill> omsSumstocktransferbills,
                                ArrayList<String> billNumbers) {

        // 1. 查找匹配的汇总单据 - 使用索引而非遍历
        int matchingBillIndex = -1;
        for (int i = 0; i < omsSumstocktransferbills.size(); i++) {
            if (sumStockTransferBillTask.judgeBillHead(omsStocktransferbill, omsSumstocktransferbills.get(i))) {
                matchingBillIndex = i;
                break;
            }
        }

        // 2. 如果没找到匹配的汇总单据，创建新的
        if (matchingBillIndex == -1) {
            OmsSumstocktransferbill newSummaryBill = new OmsSumstocktransferbill();
            sumStockTransferBillTask.initializeOneBill(omsStocktransferbill, newSummaryBill, billNumbers);

            // 为新的汇总单据创建条目
            List<OmsSumstocktransferbillentry> newEntries = new ArrayList<>();
            for (OmsStocktransferbillentry sourceEntry : omsStocktransferbill.getOmsStocktransferbillentryList()) {
                OmsSumstocktransferbillentry newSummaryEntry = new OmsSumstocktransferbillentry();
                sumStockTransferBillTask.initializeOneBillEntry(sourceEntry, newSummaryEntry);
                newEntries.add(newSummaryEntry);
            }
            newSummaryBill.setOmsSumstocktransferbillentryList(newEntries);

            omsSumstocktransferbills.add(newSummaryBill);
            return newSummaryBill.getBillnumber();
        }

        // 3. 处理匹配到的汇总单据
        OmsSumstocktransferbill matchedSummaryBill = omsSumstocktransferbills.get(matchingBillIndex);
        List<OmsSumstocktransferbillentry> summaryEntries = matchedSummaryBill.getOmsSumstocktransferbillentryList();

        // 4. 创建查找表，加速条目匹配 - 关键优化点
        Map<String, Integer> entryIndexMap = createEntryIndexMap(summaryEntries);

        // 5. 处理每个源条目
        for (OmsStocktransferbillentry sourceEntry : omsStocktransferbill.getOmsStocktransferbillentryList()) {
            // 生成条目的唯一标识
            String entryKey = generateEntryKey(sourceEntry);

            if (entryIndexMap.containsKey(entryKey)) {
                // 更新已存在的条目
                int index = entryIndexMap.get(entryKey);
                OmsSumstocktransferbillentry summaryEntry = summaryEntries.get(index);
                sumStockTransferBillTask.summaryBillEntry(sourceEntry, summaryEntry);
                summaryEntries.set(index, summaryEntry);
            } else {
                // 创建新条目
                OmsSumstocktransferbillentry newSummaryEntry = new OmsSumstocktransferbillentry();
                sumStockTransferBillTask.initializeOneBillEntry(sourceEntry, newSummaryEntry);
                summaryEntries.add(newSummaryEntry);

                // 更新索引表
                entryIndexMap.put(entryKey, summaryEntries.size() - 1);
            }
        }

        return matchedSummaryBill.getBillnumber();
    }

    // 创建条目索引映射表，用于快速查找
    private Map<String, Integer> createEntryIndexMap(List<OmsSumstocktransferbillentry> entries) {
        Map<String, Integer> indexMap = new HashMap<>();
        for (int i = 0; i < entries.size(); i++) {
            String key = generateEntryKey(entries.get(i));
            indexMap.put(key, i);
        }
        return indexMap;
    }

    // 生成条目的唯一标识键
    private String generateEntryKey(OmsSumstocktransferbillentry entry) {
        // 根据您的业务逻辑生成唯一键
        // 这里假设使用某些属性组合作为键，例如：
        String materialnumber = entry.getMaterialnumber();
        String issuewarehousenumber = entry.getIssuewarehousenumber();
        String receiptwarehousenumber = entry.getReceiptwarehousenumber();
        String unitnumber = entry.getUnitnumber();
        String baseunitnumber = entry.getBaseunitnumber();
        Date issueplandate = entry.getIssueplandate();
        Date receiptplandate = entry.getReceiptplandate();
        return materialnumber + ':' + issuewarehousenumber + ':' + receiptwarehousenumber + ':' + unitnumber + ':' + baseunitnumber + ':' + issueplandate + ':' + receiptplandate;
        // 注意：请根据实际情况调整这个键的生成逻辑，使用真正能唯一标识条目的属性组合
    }

    private String generateEntryKey(OmsStocktransferbillentry entry) {
        // 根据您的业务逻辑生成唯一键
        // 这里假设使用某些属性组合作为键，例如：
        String materialnumber = entry.getMaterialnumber();
        String issuewarehousenumber = entry.getIssuewarehousenumber();
        String receiptwarehousenumber = entry.getReceiptwarehousenumber();
        String unitnumber = entry.getUnitnumber();
        String baseunitnumber = entry.getBaseunitnumber();
        Date issueplandate = entry.getIssueplandate();
        Date receiptplandate = entry.getReceiptplandate();
        return materialnumber + ':' + issuewarehousenumber + ':' + receiptwarehousenumber + ':' + unitnumber + ':' + baseunitnumber + ':' + issueplandate + ':' + receiptplandate;
        // 注意：请根据实际情况调整这个键的生成逻辑，使用真正能唯一标识条目的属性组合
    }


    /**
     * 将库存调拨单汇总到汇总库存调拨单
     * @param omsStocktransferbill 库存调拨单
     * @param omsSumstocktransferbills 汇总库存调拨单集合
     * @param billNumbers 汇总单编码集合
     * @return 汇总单编码
     */
    public String summarySTBill2(OmsStocktransferbill omsStocktransferbill,List<OmsSumstocktransferbill> omsSumstocktransferbills,ArrayList<String> billNumbers){
        for (int i = 0; i < omsSumstocktransferbills.size(); i++) {
            OmsSumstocktransferbill omsSumstocktransferbill = omsSumstocktransferbills.get(i);
            boolean b = sumStockTransferBillTask.judgeBillHead(omsStocktransferbill, omsSumstocktransferbill);
            if (b){
                ForA :
                for (OmsStocktransferbillentry omsStocktransferbillentry : omsStocktransferbill.getOmsStocktransferbillentryList()) {
                    List<OmsSumstocktransferbillentry> omsSumstocktransferbillentryList = omsSumstocktransferbill.getOmsSumstocktransferbillentryList();
                    for (int j = 0; j < omsSumstocktransferbillentryList.size(); j++) {
                        OmsSumstocktransferbillentry omsSumstocktransferbillentry = omsSumstocktransferbillentryList.get(j);
                        boolean b1 = sumStockTransferBillTask.judgeBillEntry(omsStocktransferbillentry, omsSumstocktransferbillentry);
                        if (b1){
                            sumStockTransferBillTask.summaryBillEntry(omsStocktransferbillentry,omsSumstocktransferbillentry);
                            omsSumstocktransferbillentryList.set(j,omsSumstocktransferbillentry);
                            continue ForA;
                        }
                    }
                    OmsSumstocktransferbillentry omsSumstocktransferbillentry = new OmsSumstocktransferbillentry();
                    sumStockTransferBillTask.initializeOneBillEntry(omsStocktransferbillentry,omsSumstocktransferbillentry);
                    omsSumstocktransferbillentryList.add(omsSumstocktransferbillentry);
                    omsSumstocktransferbill.setOmsSumstocktransferbillentryList(omsSumstocktransferbillentryList);
                }
                omsSumstocktransferbills.set(i,omsSumstocktransferbill);
                return omsSumstocktransferbill.getBillnumber();
            }
        }
        OmsSumstocktransferbill omsSumstocktransferbill = new OmsSumstocktransferbill();
        sumStockTransferBillTask.initializeOneBill(omsStocktransferbill,omsSumstocktransferbill,billNumbers);
        omsSumstocktransferbills.add(omsSumstocktransferbill);
        return omsSumstocktransferbill.getBillnumber();
    }

    /**
     * 判断单据头是否可以合并
     * @param omsStocktransferbill 库存调拨单
     * @param omsSumstocktransferbill 汇总库存调拨单
     * @return 结果
     */
    public boolean judgeBillHead(OmsStocktransferbill omsStocktransferbill,OmsSumstocktransferbill omsSumstocktransferbill){
        Date bizdate = omsStocktransferbill.getBizdate();
        String issuestorageorgnumber = omsStocktransferbill.getIssuestorageorgnumber();
        String receiptstorageorgnumber = omsStocktransferbill.getReceiptstorageorgnumber();
        String outaccountcompanynumber = omsStocktransferbill.getOutaccountcompanynumber();
        String outcostcenternumber = omsStocktransferbill.getOutcostcenternumber();
        String inaccountcompanynumber = omsStocktransferbill.getInaccountcompanynumber();
        String incostcenternumber = omsStocktransferbill.getIncostcenternumber();
        String biztypenumber = omsStocktransferbill.getBiztypenumber();
        BigDecimal exchangerate = omsStocktransferbill.getExchangerate();
        String srcbilltype = omsStocktransferbill.getSrcbilltype();
        //来源系统
        String srcsys = omsStocktransferbill.getSrcsys();

        Date bizdate1 = omsSumstocktransferbill.getBizdate();
        String issuestorageorgnumber1 = omsSumstocktransferbill.getIssuestorageorgnumber();
        String receiptstorageorgnumber1 = omsSumstocktransferbill.getReceiptstorageorgnumber();
        String outaccountcompanynumber1 = omsSumstocktransferbill.getOutaccountcompanynumber();
        String outcostcenternumber1 = omsSumstocktransferbill.getOutcostcenternumber();
        String inaccountcompanynumber1 = omsSumstocktransferbill.getInaccountcompanynumber();
        String incostcenternumber1 = omsSumstocktransferbill.getIncostcenternumber();
        String biztypenumber1 = omsSumstocktransferbill.getBiztypenumber();
        BigDecimal exchangerate1 = omsSumstocktransferbill.getExchangerate();
        String description = omsSumstocktransferbill.getDescription();
        String srcsys1 = omsSumstocktransferbill.getSrcsys();

        //单据类型
        boolean boolean10 = OmsTaskUtil.juedeString(srcbilltype, description);
        //判断单据类型是否一致，如果一致则继续判断
        //若不同则继续判断
        if (!boolean10){
            return false;
        }
        //业务日期
        boolean boolean1 = OmsTaskUtil.juedeDate(bizdate,bizdate1);
        //判断调出库存组织编码是否一致
        boolean boolean2 = OmsTaskUtil.juedeString(issuestorageorgnumber,issuestorageorgnumber1);
        //判断调入库存组织编码是否一致
        boolean boolean3 = OmsTaskUtil.juedeString(receiptstorageorgnumber,receiptstorageorgnumber1);
        //调出记账公司
        boolean boolean4 = OmsTaskUtil.juedeString(outaccountcompanynumber,outaccountcompanynumber1);
        //调出成本中心
        boolean boolean5 = OmsTaskUtil.juedeString(outcostcenternumber,outcostcenternumber1);
        //调入记账公司
        boolean boolean6 = OmsTaskUtil.juedeString(inaccountcompanynumber,inaccountcompanynumber1);
        //调入成本中心
        boolean boolean7 = OmsTaskUtil.juedeString(incostcenternumber,incostcenternumber1);
        //业务类型编码
        boolean boolean8 = OmsTaskUtil.juedeString(biztypenumber,biztypenumber1);
        //汇率
        boolean boolean9 = OmsTaskUtil.juedeBigDecimal(exchangerate,exchangerate1);
        //来源系统
        boolean boolean11 = OmsTaskUtil.juedeString(srcsys, srcsys1);

        return boolean1&&boolean2&&boolean3&&boolean4&&boolean5&&boolean6&&boolean7&&boolean8&&boolean9&&boolean11;

    }

    /**
     * 判断分录是否可以合并
     * @param stocktransferbillentry 库存调拨单分录
     * @param sumstocktransferbillentry 汇总库存调拨单分录
     * @return 结果
     */
    public boolean judgeBillEntry(OmsStocktransferbillentry stocktransferbillentry, OmsSumstocktransferbillentry sumstocktransferbillentry){
        String materialnumber = stocktransferbillentry.getMaterialnumber();
        String issuewarehousenumber = stocktransferbillentry.getIssuewarehousenumber();
        String receiptwarehousenumber = stocktransferbillentry.getReceiptwarehousenumber();
        String unitnumber = stocktransferbillentry.getUnitnumber();
        String baseunitnumber = stocktransferbillentry.getBaseunitnumber();
        Date issueplandate = stocktransferbillentry.getIssueplandate();
        Date receiptplandate = stocktransferbillentry.getReceiptplandate();

        String materialnumber1 = sumstocktransferbillentry.getMaterialnumber();
        String issuewarehousenumber1 = sumstocktransferbillentry.getIssuewarehousenumber();
        String receiptwarehousenumber1 = sumstocktransferbillentry.getReceiptwarehousenumber();
        String unitnumber1 = sumstocktransferbillentry.getUnitnumber();
        String baseunitnumber1 = sumstocktransferbillentry.getBaseunitnumber();
        Date issueplandate1 = sumstocktransferbillentry.getIssueplandate();
        Date receiptplandate1 = sumstocktransferbillentry.getReceiptplandate();

        boolean boolean1 = materialnumber.equals(materialnumber1);
        boolean boolean2 = issuewarehousenumber.equals(issuewarehousenumber1);
        boolean boolean3 = receiptwarehousenumber.equals(receiptwarehousenumber1);
        boolean boolean4 = unitnumber.equals(unitnumber1);
        boolean boolean5 = baseunitnumber.equals(baseunitnumber1);
        boolean boolean6 = issueplandate.equals(issueplandate1);
        boolean boolean7 = receiptplandate.equals(receiptplandate1);

        return boolean1&&boolean2&&boolean3&&boolean4&&boolean5&&boolean6&&boolean7;
    }

    public boolean  judgeSumBillEntry(OmsSumstocktransferbillentry omsSumstocktransferbillentry1, OmsSumstocktransferbillentry omsSumstocktransferbillentry2){
        String materialnumber1 = omsSumstocktransferbillentry1.getMaterialnumber();
        String issuewarehousenumber1 = omsSumstocktransferbillentry1.getIssuewarehousenumber();
        String receiptwarehousenumber1 = omsSumstocktransferbillentry1.getReceiptwarehousenumber();
        String unitnumber1 = omsSumstocktransferbillentry1.getUnitnumber();
        String baseunitnumber1 = omsSumstocktransferbillentry1.getBaseunitnumber();
        Date issueplandate1 = omsSumstocktransferbillentry1.getIssueplandate();
        Date receiptplandate1 = omsSumstocktransferbillentry1.getReceiptplandate();


        String materialnumber2 = omsSumstocktransferbillentry2.getMaterialnumber();
        String issuewarehousenumber2 = omsSumstocktransferbillentry2.getIssuewarehousenumber();
        String receiptwarehousenumber2 = omsSumstocktransferbillentry2.getReceiptwarehousenumber();
        String unitnumber2 = omsSumstocktransferbillentry2.getUnitnumber();
        String baseunitnumber2 = omsSumstocktransferbillentry2.getBaseunitnumber();
        Date issueplandate2 = omsSumstocktransferbillentry2.getIssueplandate();
        Date receiptplandate2 = omsSumstocktransferbillentry2.getReceiptplandate();

        boolean boolean1 = materialnumber1.equals(materialnumber2);
        boolean boolean2 = issuewarehousenumber1.equals(issuewarehousenumber2);
        boolean boolean3 = receiptwarehousenumber1.equals(receiptwarehousenumber2);
        boolean boolean4 = unitnumber1.equals(unitnumber2);
        boolean boolean5 = baseunitnumber1.equals(baseunitnumber2);
        boolean boolean6 = issueplandate1.equals(issueplandate2);
        boolean boolean7 = receiptplandate1.equals(receiptplandate2);

        return boolean1&&boolean2&&boolean3&&boolean4&&boolean5&&boolean6&&boolean7;
    }



        /**
         * 合并库存调拨单分录
         * @param stocktransferbillentry 库存调拨单分录
         * @param sumstocktransferbillentry 汇总库存调拨单分录
         */
    public void summaryBillEntry(OmsStocktransferbillentry stocktransferbillentry,OmsSumstocktransferbillentry sumstocktransferbillentry){
        BigDecimal qty = stocktransferbillentry.getQty();
        BigDecimal baseqty = stocktransferbillentry.getBaseqty();

        sumstocktransferbillentry.setQty(sumstocktransferbillentry.getQty().add(qty));
        sumstocktransferbillentry.setBaseqty(sumstocktransferbillentry.getBaseqty().add(baseqty));

    }

    public void summarySumBillEntry(OmsSumstocktransferbillentry omsSumstocktransferbillentry1,OmsSumstocktransferbillentry omsSumstocktransferbillentry2){
        BigDecimal qty = omsSumstocktransferbillentry1.getQty();
        BigDecimal baseqty = omsSumstocktransferbillentry1.getBaseqty();

        omsSumstocktransferbillentry2.setQty(omsSumstocktransferbillentry2.getQty().add(qty));
        omsSumstocktransferbillentry2.setBaseqty(omsSumstocktransferbillentry2.getBaseqty().add(baseqty));
    }



        /**
         * 初始化汇总库存调拨单
         * @param stocktransferbill 库存调拨单
         * @param sumstocktransferbill 汇总库存调拨单
         * @param billNumbers 汇总单据编码
         */
    public void initializeOneBill(OmsStocktransferbill stocktransferbill,OmsSumstocktransferbill sumstocktransferbill,ArrayList<String> billNumbers){
        Date bizdate = stocktransferbill.getBizdate();
        String biztypenumber = stocktransferbill.getBiztypenumber();
        String biztypename = stocktransferbill.getBiztypename();
        String issuestorageorgnumber = stocktransferbill.getIssuestorageorgnumber();
        String issuestorageorgname = stocktransferbill.getIssuestorageorgname();
        String receiptstorageorgnumber = stocktransferbill.getReceiptstorageorgnumber();
        String receiptstorageorgname = stocktransferbill.getReceiptstorageorgname();
        String outcostcenternumber = stocktransferbill.getOutcostcenternumber();
        String outcostcentername = stocktransferbill.getOutcostcentername();
        String outaccountcompanynumber = stocktransferbill.getOutaccountcompanynumber();
        String outaccountcompanyname = stocktransferbill.getOutaccountcompanyname();
        String incostcenternumber = stocktransferbill.getIncostcenternumber();
        String incostcentername = stocktransferbill.getIncostcentername();
        String inaccountcompanynumber = stocktransferbill.getInaccountcompanynumber();
        String inaccountcompanyname = stocktransferbill.getInaccountcompanyname();
        String currency = stocktransferbill.getCurrency();
        BigDecimal exchangerate = stocktransferbill.getExchangerate();
        String srcbilltype = stocktransferbill.getSrcbilltype();
        //来源系统
        String srcsys = stocktransferbill.getSrcsys();

        sumstocktransferbill.setBizdate(bizdate);
        String billNumber = sumStockTransferBillTask.getNextBillNumber(sumstocktransferbill, billNumbers);
        sumstocktransferbill.setBillnumber(billNumber);
        sumstocktransferbill.setBiztypenumber(biztypenumber);
        sumstocktransferbill.setBiztypename(biztypename);
        sumstocktransferbill.setIssuestorageorgnumber(issuestorageorgnumber);
        sumstocktransferbill.setIssuestorageorgname(issuestorageorgname);
        sumstocktransferbill.setReceiptstorageorgnumber(receiptstorageorgnumber);
        sumstocktransferbill.setReceiptstorageorgname(receiptstorageorgname);
        sumstocktransferbill.setOutcostcenternumber(outcostcenternumber);
        sumstocktransferbill.setOutcostcentername(outcostcentername);
        sumstocktransferbill.setOutaccountcompanynumber(outaccountcompanynumber);
        sumstocktransferbill.setOutaccountcompanyname(outaccountcompanyname);
        sumstocktransferbill.setIncostcenternumber(incostcenternumber);
        sumstocktransferbill.setIncostcentername(incostcentername);
        sumstocktransferbill.setInaccountcompanynumber(inaccountcompanynumber);
        sumstocktransferbill.setInaccountcompanyname(inaccountcompanyname);
        sumstocktransferbill.setCurrency(currency);
        sumstocktransferbill.setExchangerate(exchangerate);
        sumstocktransferbill.setDescription(srcbilltype);
        sumstocktransferbill.setCreatetime(new Date());
        sumstocktransferbill.setIstrans("0");
        //来源系统
        sumstocktransferbill.setSrcsys(srcsys);

        List<OmsStocktransferbillentry> omsStocktransferbillentryList = stocktransferbill.getOmsStocktransferbillentryList();
        ArrayList<OmsSumstocktransferbillentry> omsSumstocktransferbillentries = new ArrayList<>();
        if (StringUtils.isEmpty(omsStocktransferbillentryList)){
            sumstocktransferbill.setOmsSumstocktransferbillentryList(omsSumstocktransferbillentries);
            return;
        }
        for (OmsStocktransferbillentry omsStocktransferbillentry : omsStocktransferbillentryList) {
            OmsSumstocktransferbillentry omsSumstocktransferbillentry = new OmsSumstocktransferbillentry();
            sumStockTransferBillTask.initializeOneBillEntry(omsStocktransferbillentry,omsSumstocktransferbillentry);
            omsSumstocktransferbillentries.add(omsSumstocktransferbillentry);
        }
        sumstocktransferbill.setOmsSumstocktransferbillentryList(omsSumstocktransferbillentries);
    }

    /**
     * 初始化汇总库存调拨单分录
     * @param stocktransferbillentry 库存调拨单分录
     * @param sumstocktransferbillentry 汇总库存调拨单分录
     */
    public void initializeOneBillEntry(OmsStocktransferbillentry stocktransferbillentry,OmsSumstocktransferbillentry sumstocktransferbillentry){
        String materialnumber = stocktransferbillentry.getMaterialnumber();
        String materialname = stocktransferbillentry.getMaterialname();
        String issuewarehousenumber = stocktransferbillentry.getIssuewarehousenumber();
        String issuewarehousename = stocktransferbillentry.getIssuewarehousename();
        String receiptwarehousenumber = stocktransferbillentry.getReceiptwarehousenumber();
        String receiptwarehousename = stocktransferbillentry.getReceiptwarehousename();
        String unitnumber = stocktransferbillentry.getUnitnumber();
        String baseunitnumber = stocktransferbillentry.getBaseunitnumber();
        BigDecimal qty = stocktransferbillentry.getQty();
        BigDecimal baseqty = stocktransferbillentry.getBaseqty();
        Date issueplandate = stocktransferbillentry.getIssueplandate();
        Date receiptplandate = stocktransferbillentry.getReceiptplandate();
        Long ispresent = stocktransferbillentry.getIspresent();

        sumstocktransferbillentry.setMaterialnumber(materialnumber);
        sumstocktransferbillentry.setMaterialname(materialname);
        sumstocktransferbillentry.setIssuewarehousenumber(issuewarehousenumber);
        sumstocktransferbillentry.setIssuewarehousename(issuewarehousename);
        sumstocktransferbillentry.setReceiptwarehousenumber(receiptwarehousenumber);
        sumstocktransferbillentry.setReceiptwarehousename(receiptwarehousename);
        sumstocktransferbillentry.setUnitnumber(unitnumber);
        sumstocktransferbillentry.setBaseunitnumber(baseunitnumber);
        sumstocktransferbillentry.setQty(qty);
        sumstocktransferbillentry.setBaseqty(baseqty);
        sumstocktransferbillentry.setIssueplandate(issueplandate);
        sumstocktransferbillentry.setReceiptplandate(receiptplandate);
        sumstocktransferbillentry.setIspresent(ispresent);

    }

    /**
     * 根据业务日期以及已经生成的汇总单编码获取单据编码
     * @param sumstocktransferbill 汇总库存调拨单
     * @param billNumbers 汇总单据编码集合
     * @return 汇总单编码
     */
    public String getNextBillNumber(OmsSumstocktransferbill sumstocktransferbill,ArrayList<String> billNumbers){
        //时间字符串产生方式
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        //组合流水号前一部分
        String billNumberPfix = format.format(sumstocktransferbill.getBizdate());
        if (StringUtils.isEmpty(billNumbers)){
            return omsTaskUtil.setBillNumber(sumstocktransferbill);
        }else {
            for (int i = billNumbers.size()-1; i >= 0; i--){
                String billNumber = billNumbers.get(i);
                if (billNumber.contains(billNumberPfix)){
                    //从单据编号中截取后6位
                    String billNumberEnd = billNumber.substring(billNumber.length() - 6);
                    int endNumber = Integer.parseInt(billNumberEnd);
                    int tempNumber = 1000000 + endNumber + 1;
                    //将首位的1去掉之后，拼接成流水号
                    billNumber = "HZKCDB" + '-' + billNumberPfix + '-' + String.valueOf(tempNumber).substring(1);
                    return billNumber;
                }
            }
            return omsTaskUtil.setBillNumber(sumstocktransferbill);
        }
    }

}
